//
//  WSEmoticonViewModel.swift
//  emoticonView
//
//  Created by 武莎莎 on 15/9/15.
//  Copyright © 2015年 itcast. All rights reserved.
//

import UIKit

// 通过模型，加载表情数据
class WSEmoticonViewModel: NSObject {
    // 表情包数组
    lazy var packages:[WSEmoticonPackage] = [WSEmoticonPackage]()
    
    // 从bundle 中取出字典中需要的数据，并转化为WSEmoticonPackage模型输出
    func loadPackages() {
        
        let path = NSBundle.mainBundle().pathForResource("emoticons", ofType: "plist", inDirectory: "Emoticons.bundle")
        let dict = NSDictionary(contentsOfFile: path!)
        let array = dict!["packages"] as! [[String:AnyObject]]
        
        packages.append(WSEmoticonPackage(dict: ["group_name_cn":"最近"]))
        for ids in array {
            // 获取表情包的id
            let id = ids["id"] as! String  
            // 拼接表情包的路径
            let emPath = NSBundle.mainBundle().pathForResource("info", ofType: "plist", inDirectory: "Emoticons.bundle/" + id)
            let emDict = NSDictionary(contentsOfFile: emPath!) as! [String:AnyObject]
            
            packages.append(WSEmoticonPackage(dict: emDict))
        
        }
    }
    
    func emoticonWithIndexpath(indexPath:NSIndexPath)->WSEmoticons{
        return packages[indexPath.section].emoticons[indexPath.item]
    }
}
